package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.Schooltimetable;
import henu.dao.ISchooltimetableDao;
/**
 * @author 纪柳任
 * 2016年9月19日下午10:28:58
 */
public class SchooltimetableDaoImpl implements ISchooltimetableDao{


	protected QueryRunner queryRunner =new QueryRunner();
    private int rows;
	public SchooltimetableDaoImpl(QueryRunner queryRunner) {
		super();
		this.queryRunner = queryRunner;
	}
	@Override
	public Schooltimetable querry(Schooltimetable schooltimetable) {
		String sql = "select * from tb_schooltimetable where fNumber=?";
		Object param[]={schooltimetable.getfNumber()};
		
		Schooltimetable schooltimetables= null;
		try {
			schooltimetables= queryRunner.query(sql, param,new BeanHandler<Schooltimetable>(Schooltimetable.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return schooltimetables;
	}
	@Override
	public void add(Schooltimetable schooltimetable) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_schooltimetable() values(?,?,?,?,?)";
		Object[] param ={schooltimetable.getfNumber(),schooltimetable.getfTeachingClassNumber(),schooltimetable.getfClassTime(),schooltimetable.getfClassSpot(),schooltimetable.getfMemo()};
		try {
			rows=queryRunner.update(sql, param);
        System.out.println(rows+"行已经发生变化");
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
	@Override
	public void delete(Schooltimetable schooltimetable) {
		// TODO Auto-generated method stub
		String sql="delete from tb_schooltimetable where fNumber=?";
		Object[] param={schooltimetable.getfNumber()};
		try {
			rows=queryRunner.update(sql,param);
			System.out.println(rows+"行已经发生变化");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void update(Schooltimetable schooltimetable) {
		// TODO Auto-generated method stub
		String sql="update tb_schooltimetable set fNumber=?,fTeachingClassNumber=?,fClassTime=?,fClassSpot=?,fMemo=?";
		Object param[]={schooltimetable.getfNumber(),schooltimetable.getfTeachingClassNumber(),schooltimetable.getfClassTime(),schooltimetable.getfClassSpot(),schooltimetable.getfMemo()};
		try {
			rows=queryRunner.update(sql,param);
			System.out.println(rows+"已经发生变化");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Override
	public List<Schooltimetable> getAll() {
		String sql = "select * from tb_schooltimetable";
		List<Schooltimetable> schooltimetables = null;		
		try {
			schooltimetables= queryRunner.query(sql, new BeanListHandler<Schooltimetable>(Schooltimetable.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return schooltimetables;
	}
	
}
